<?php
namespace frontend\services;
use  yii\helpers\Html;

class DataHelper {

    public static function encode($dispaly_text){
        return  Html::encode($dispaly_text);
    }

	public static function getIP(){
		if ( !empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ){
			return $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		return isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"]:'';
	}
    /**
     *分页
     */
    public static function ipagination($params)
    {
        $ret = [
            'previous'  => true,
            'next'      => true,
            'from'      => 0,
            'end'       => 0,
            'totalPage' => 0,
            'total_page' => 0,
            'current'   => 0,
            'page_size' => intval( $params['page_size'] )
        ];
        $total     = (int)$params['total_count'];
        $pageSize  = (int)$params['page_size'];
        $page      = (int)$params['page'];
        $display   = (int)$params['display'];
        $totalPage = (int)ceil($total / $pageSize);
        $totalPage = $totalPage?$totalPage:1;

        if ($page <= 1) {
            $ret['previous'] = false;
        }
        if ($page >= $totalPage) {
            $ret['next'] = false;
        }
        $semi = (int)ceil($display / 2);
        if ($page - $semi > 0) {
            $ret['from'] = $page - $semi;
        } else {
            $ret['from'] = 1;
        }
        if ($page + $semi <= $totalPage) {
            $ret['end'] = $page + $semi;
        } else {
            $ret['end'] = $totalPage;
        }
        $ret['total_count'] = $total;
        $ret['totalPage'] = $totalPage;
        $ret['total_page'] = $totalPage;
        $ret['current']   = $page;
        return $ret;
    }
    /**
     * 设置过期时间和返回手机接收码
     * @param $length
     * @return null|string
     */
    public static function hidtel($phone)
    {
        //屏蔽电话号码中间的四位数字
        $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话
        if ($IsWhat == 1) {
            return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone);

        } else {
            return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone);
        }

    }
}
